# -*- coding: utf-8 -*-

import scrapy

import requests
from datascrapy.items import TableInfoItem
from datascrapy.settings import URL
from datascrapy.settings import COOKIE
from datascrapy.settings import HEADERS
from datascrapy.settings import DATAHEADERS
from datascrapy.settings import ALLOWED_DOMAINS
from datascrapy.settings import START_URLS
from datascrapy.util import cookieutil


class SlowsqlSpider(scrapy.Spider):
    name = 'tableinfo'
    allowed_domains = ALLOWED_DOMAINS
    start_urls = START_URLS

    def parse(self, response):
        response = requests.get(URL, headers=HEADERS)
        cookieutil.set_response_cookie(response)
        id_str = "106804533,106762494,106796113,106820476,106820473,106820479"
        hos_tstr = "10.96.171.50,10.96.2.143,10.94.113.90,10.94.116.169,10.94.113.31,10.94.111.46"
        name = "jwmsprd16"

        index = 0
        for id in id_str.split(","):
            host = hos_tstr.split(",")[index]
            index = index + 1
            is_main_db_url = "http://dbms.itsm.cxxxxs.com/object-browser/table?" + "db=" + id + "&type=3" + "&host=" + host + "&name=" + name
            DATAHEADERS["Referer"] = is_main_db_url
            DATAHEADERS["Cookie"] = COOKIE + cookieutil.get_response_cookie()
            is_main_response = requests.post(is_main_db_url, data=get_payload_param(), headers=DATAHEADERS)
            is_main_response_data= is_main_response.json()
            for valueList in is_main_response_data["aaData"]:
                table_info = TableInfoItem()
                table_info["dbName"] = host
                table_info["tableName"] = valueList[2]
                table_info["engineName"] = valueList[4]
                table_info["tableRow"] = valueList[5]
                table_info["tableSize"] = valueList[6]
                yield table_info


def get_payload_param():
    json_str = '[{"name": "draw", "value": 1},{"name": "columns", "value": [{"data": 0, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 1, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 2, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 3, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 4, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 5, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 6, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 7, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 8, "name": "", "searchable": true, "orderable": false, "search": {"value": "", "search": false}},{"data": 9, "name": "", "searchable": true, "orderable": false, "search": {"value": "", "search": false}}]},{"name": "order", "value": [{"column": 6, "dir": "desc"}]},{"name": "start", "value": 0},{"name": "length", "value": 10000},{"name": "search", "value": {"value": "", "search": false}},{"fliter": {"classify": "common", "runtime": "7", "outtime": "", "keytext": ""}}]'
    return json_str
